import {defineStore} from "pinia";
import type {UserState} from "@/stores/modules/model/userModel";
import type {UserInfo} from "@/api/user/types";
import pinia from "@/stores";
import {getUserInfo, logout} from "@/api";
import {useAuthStoreWithOut} from "@/stores/modules/auth";
import {RESETSTORE} from "@/utils/reset";


export const useUserStore = defineStore('app-user',{
    state:(): UserState => ({
        token : '',
        userInfo : null
    }),
    actions:{
        // 设置 token
        setToken(token: string) {
            this.token = token;
        },
        setUserInfo(userInfo: UserInfo) {
            this.userInfo = userInfo;
        },
        async getInfoActon(){
            const { data } = await getUserInfo();
            const { avatar, buttons, name, roles, routes } = data
            const authStore = useAuthStoreWithOut();
            // 存储用户信息
            this.setUserInfo({avatar, name});
            // 存储用户权限信息
            authStore.setAuth({buttons, roles, routes})
        },
        async logout(){
            await logout();
            RESETSTORE();
        }
    },

    persist: true
})

export const useUserStoreWithOut = ()=>{
    return useUserStore(pinia);
}